/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.swing.JCheckBox;

import org.hibernate.Session;
import org.springframework.stereotype.Service;

import com.ruipengkj.bean.TRight;
import com.ruipengkj.bean.TRole;
import com.ruipengkj.bean.TRoleRight;
import com.ruipengkj.commons.base.query.BaseQuery;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.dao.TRoleRightDao;
import com.ruipengkj.dao.TRoleRightIMDao;
import com.ruipengkj.service.TRoleRightIMService;
/**
 * 说明：
 * 角色权限表dao层接口
 * @Author：李智深
 * @Date：2012-03-16
 */
@Service
public class TRoleRightIMServiceImpl implements TRoleRightIMService{
	@Resource
	private TRoleRightIMDao TRoleRightIMDao;//DAO层
	@Resource
	private TRoleRightDao TRoleRightDao;//DAO层
	
	public Session session = null;   
	//查询所有
	@Override
	public List<TRoleRight> findService(TRoleRight entity) throws Exception {
		String hql = "select o from TRoleRight o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if(whereHQL.length()>4)
				whereHQL.delete(whereHQL.length()-5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TRoleRightIMDao.findDao(hql, whereHQL.toString(), param, orderby);
	}
	//根据ID翻页查询
	@Override
	public PageModel<TRoleRight> getScrollService(int firstResult,
			int maxResult, TRoleRight entity) throws Exception {
		String hql = "select o from TRoleRight o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if(whereHQL.length()>4)
				whereHQL.delete(whereHQL.length()-5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TRoleRightIMDao.getScrollDao(hql, whereHQL.toString(), param, firstResult, maxResult, orderby);
	}

	//修改
	@Override
	public void modifyService(TRoleRight entity) throws Exception {
		TRoleRightIMDao.modifyDao(entity);
		
	}
	//添加
	@Override
	public void saveService(TRoleRight entity) throws Exception {
		TRoleRightIMDao.saveDao(entity);
	}
	//添加或修改
	@Override
	public void saveOrUpdateService(TRoleRight entity) throws Exception {
		TRoleRightIMDao.saveOrUpdateDao(entity);
	}
	//添加返回ID
	@Override
	public Serializable saveRetService(TRoleRight entity) throws Exception {
		return TRoleRightIMDao.saveRetDao(entity);
	}
	public TRoleRightIMDao getTRoleRightDao() {
		return TRoleRightIMDao;
	}

	public void setTRoleRightDao(TRoleRightDao TRoleRightDao) {
		this.TRoleRightIMDao = TRoleRightIMDao;
	}
	@Override
	public void deleteService(String id) throws Exception {
		TRoleRightIMDao.deleteDao(id);
		
	}
	@Override
	public TRoleRight getByIdService(String id) throws Exception {
		
		return TRoleRightIMDao.getByIdDao(id);
	}
	@Override
	public Object executeHQL(String id) throws Exception {
		String hql = "delete from TRoleRight";
		List<Object> param = new ArrayList<Object>();
		StringBuffer whereHQL = new StringBuffer();	
			whereHQL.append("roleID").append(BaseQuery.EQ).append("'"+id+"'");
			param.add(id);
		return TRoleRightIMDao.executeHQLDao(hql, "", whereHQL.toString(), null);
	}
	@Override
	public void saveRoleRight(String JRoleID,List<TRight> tRightList,JCheckBox []jCheckBox,TRole tRole) throws Exception {
		// TODO Auto-generated method stub
		executeHQL(JRoleID);
		int subMenuCount=0;
		int mainEnumIdx=0;
		for(int i=0;i<tRightList.size();i++){
			if(jCheckBox[i] == null)
			{
				if(tRightList.get(i).getLevel()<10){
					mainEnumIdx = i;
					subMenuCount = 0;
				}
				continue;
			}
			if (jCheckBox[i].isSelected()) {			
				if(subMenuCount==0){
					TRoleRight roleRight=new TRoleRight();
					roleRight.setTRole(tRole);
					roleRight.setTRight(tRightList.get(mainEnumIdx));
					TRoleRightDao.saveDao(roleRight);
				}
				
			Object obj1=tRightList.get(i);
			TRoleRight roleRight=new TRoleRight();
			roleRight.setTRole(tRole);
			roleRight.setTRight((TRight)obj1);
			TRoleRightDao.saveDao(roleRight);
			subMenuCount ++;
			}
		}
	}
}